package org.monster.context.core.response;

import org.monster.context.core.parameter.HttpResponse;
import org.monster.context.core.processor.Process;
import org.monster.context.core.request.HttpRequest;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 响应工厂
 *
 * @author Monster
 */
public class ResponseFactory {

    /**
     * 日志器
     */
    private static final Logger logger = Logger.getLogger(ResponseFactory.class.getName());
    /**
     * 关闭
     */
    private static final String SHUTDOWN_PROCESS = "SHUTDOWN";

    /**
     * 构造方法
     */
    private ResponseFactory() {
    }

    /**
     * 获取 状态
     *
     * @param request  请求
     * @param response 响应
     * @return
     */
    public static String getState(HttpRequest request, HttpResponse response) {
        String requestURI = request.getRequestURI();
        String mime = response.getMime();
        if (mime != null && requestURI.indexOf('.') != -1) {
            return ResponseState.CLASS_RESOURCE;
        } else if (requestURI.equals("\u002f" + SHUTDOWN_PROCESS)) {
            return ResponseState.CLASS_SERVICE;
        } else {
            return ResponseState.CLASS_SERVLET;
        }
    }

    /**
     * 获取 实例
     *
     * @param state 状态
     * @return 处理
     * @throws ClassNotFoundException 类未找到异常
     */
    public static Process getInstance(String state)
            throws ClassNotFoundException {
        Process processor = null;
        try {
            processor = (Process) Class.forName(state).newInstance();
        } catch (InstantiationException | IllegalAccessException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return processor;
    }
}
